#!/usr/bin/env python
# coding: utf-8

# # 高德API函数
# * 准备工作

# In[1]:


import requests,json
import pandas as pd
from PIL import Image
from io import BytesIO
coco_key = "b31da81c9c868d1da2b0a1b7fc1419b4"


# ## 地理编码(geocode)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/georegeo#geo

# In[2]:


def geocode(key,address,city=None,batch=None,sig=None,callback=None):
    """获取高德API的地理编码"""
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":key,
        "address":address,
        "city":city,
        "batch":batch,
        "sig":sig,
        "callback":callback,
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0][ 'location']
    return results


# ## 逆地理编码(regeo)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/georegeo#regeo

# In[3]:


def regeo(key,location,poitype=None,radius=1000,extensions="base",batch=None,output=None,homeorcorp=None,roadlevel=None,sig=None,callback=None):
    """获取高德API的逆地理编码"""
    regeo_url = "https://restapi.amap.com/v3/geocode/regeo"
    params = {
        "key":key,
        "location":location,
        "poitype":poitype,
        "radius":radius,
        "extensions":extensions,
        "batch":batch,
        "output": "json",
        "homeorcorp": homeorcorp,
        "roadlevel":roadlevel,
        "sig":sig,
        "callback":callback,
    }
    r = requests.get(regeo_url,params=params)
    results = r.json()[ 'regeocode'][ 'formatted_address']
    return results


# ## 步行路径(walking)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/direction#walk

# In[4]:


def walking(key,origin,destination,sig=None,output=None,callback=None):
    """获取高德API的步行路径"""
    walking_url = "https://restapi.amap.com/v3/direction/walking"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
         "output":"json",
        "sig":sig,
        "callback":callback
    }
    r=requests.get(walking_url,params=params)
    results = r.json()['route'][ 'paths'][0]['steps']
    return results


# ## 公交车路径(integrated)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/direction#bus

# In[54]:


def integrated(key,origin,destination,city,cityd=None,extensions=None,strategy=None,nightflag=None,date=None,time=None,output=None,callback=None):
    """获取高德API的公交车路径"""
    url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
      "key": key,
        "origin": origin,
        "destination": destination,
        "city": city,
        "cityd": cityd,
        "extensions": extensions,
        "strategy": strategy,
        "nightflag": nightflag,
        "date": date,
        "time": time,
        "output": "json",
        "callback": callback
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'route']['transits'][0][ 'segments'][0]['bus']['buslines'][0]
    return results


# ## 驾车路径(driving)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/direction#driving

# In[6]:


def driving(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None,strategy=None,waypoints=None,avoidpolygons=None,avoidroad=None,province=None,number=None,cartype=0,ferry=0,roadaggregation=None,nosteps=None,sig=None,output=None,callback=None,extensions=None):
    """获取高德API的驾车路径"""
    url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "originid":originid,
        "destinationid":destinationid,
        "origintype":origintype,
        "destinationtype":destinationtype,
        "strategy":strategy,
        "waypoints":waypoints,
        "avoidpolygons":avoidpolygons,
        "avoidroad":avoidroad,
        "province":province,
        "number":number,
        "cartype":cartype,
        "ferry":ferry,
        "roadaggregation":roadaggregation,
        "nosteps":nosteps,
        "sig":sig,
        "output":json,
        "callback":callback,
        "extensions":extensions
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'route']['paths'][0][ 'steps']
    return results


# ## 骑行路径(bicycling)
# * 参考文件：https://lbs.amap.com/api/webservice/guide/api/direction#t8

# In[7]:


def bicycling(key,origin,destination):
    """获取高德API的骑行路径"""
    url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    response = requests.get(url,params=params)
    results = response.json()['data'][ 'paths'][0][ 'steps']
    return results


# ## 货车路径(truck)
# * 参考文件：https://lbs.amap.com/api/webservice/guide/api/direction#t9

# In[8]:


def truck(key,origin,destination,size=2,originid=None,originidtype=None,destinationid=None,destinationtype=None,diu=None,strategy=1,waypoints=None,height=1.6,width=2.5,load=10,weight=0.9,axis=2,province=None,number=None,cartype=0,avoidpolygons=None,showpolyline=1,nosteps=0):
    """获取高德API的货车路径"""
    url = "https://restapi.amap.com/v4/direction/truck?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "size":size,
        "originid":originid,
        "originidtype":originidtype,
        "destinationid":destinationid,
        "destinationtype":destinationtype,
        "diu":diu,
        "strategy":strategy,
        "waypoints":waypoints,
        "height":height,
        "width":width,
        "load":load,
        "weight":weight,
        "axis":axis,
        "province":province,
        "number":number,
        "cartype":cartype,
        "avoidpolygons":avoidpolygons,
        "showpolyline":showpolyline,
        "nosteps":nosteps,
    }
    response = requests.get(url,params=params)
    results = response.json()['data']['route'][ 'paths'][0][ 'steps']
    return results


# ## 物流距离测量矩阵(distance)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/direction#distance

# In[9]:


def distance(key,origins,destination,type=1,sig=None,output=None,callback=None):
    """获取高德API的物流距离测量功能"""
    url = "https://restapi.amap.com/v3/distance?parameters"
    params = {
        "key":key,
        "origins":origins,
        "destination":destination,
        "type":type,
         "sig":sig,
         "output":json,
        "callback":callback
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'results'][0]['distance']
    return results


# ## 行政区域查询(district)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/district

# In[10]:


def district(key,keywords=None,subdistrict=None,page=None,offset=None,extensions=None,filter=None,callback=None,output=None):
    """获取高德API的行政区域查询"""
    district_url = "https://restapi.amap.com/v3/config/district"
    params = {"key":key,
              "output":"json",
              "keywords":keywords,
              "subdistrict":subdistrict,
              "page":page,
              "offset":offset,
              "extensions":extensions,
              "filter":filter,
              "callback":callback,
    }
    r = requests.get(district_url,params=params)
    results = r.json()[ 'districts'][0][ 'districts']
    return results


# ## 搜索POI(text)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/search

# In[11]:


def text(key,keywords=None,types=None,city=None,citylimit=None,children=None,offset=None,page=None,extensions=None,sig=None,output=None,callback=None):
    """获取高德API的搜索POI"""
    text_url = "https://restapi.amap.com/v3/place/text"
    params = {
        "key":key,
        "keywords":keywords,
        "city":"110000",
        "output":"json",
        "types":types,
        "citylimit":citylimit,
        "children":children,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "callback":callback,
    }
    r = requests.get(text_url,params=params)
    results = r.json()[ 'pois']
    return results


# ## IP定位(ip)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/ipconfig

# In[12]:


def ip(key,ip=None,sig=None,output=None):
    """获取高德API的IP定位"""
    ip_url = "https://restapi.amap.com/v3/ip"
    params = {
        "key":key,
        "output":json,
        "sig":sig
    }
    r = requests.get(ip_url,params=params)
    results = r.json()[ 'rectangle']
    return results


# ## 静态地图(staticmap)
# * https://lbs.amap.com/api/webservice/guide/api/staticmaps

# In[13]:


def staticmap(key,location,zoom=None,size="400*400",scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    """获取高德API的静态地图"""
    url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key":key,
        "location":location,
        "zoom":zoom,
        "size":size,
        "scale":scale,
        "markers":markers,
        "labels":labels,
        "paths":paths,
        "traffic":traffic,
        "sig":sig
    }
    response = requests.get(url,params=params)
    results = Image.open(BytesIO(response.content))
    return results


# ## 坐标转换(convert)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/convert

# In[14]:


def convert(key,locations,coordsys=None,sig=None,output=None):
    """将非高德坐标转换为高德坐标"""
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key":key,
        "locations":locations,
        "coordsys":coordsys,
        "sig":sig,
        "output":output
    }
    response = requests.get(url,params=params)
    result = response.json()[ 'locations']
    return result


# ## 天气查询(weatherInfo)
# * https://lbs.amap.com/api/webservice/guide/api/weatherinfo

# In[15]:


def weatherInfo(key,city,extensions=None,output=None):
    """获取高德API的天气查询功能"""
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":key,
        "city":city,
        "extensions":extensions,
        "output":json
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'lives'][0]
    return results


# ## 输入提示(inputtips)
# * 参考文档：https://lbs.amap.com/api/webservice/guide/api/inputtips

# In[16]:


def inputtips(key,keywords,type=None,location=None,city=None,citylimit=None,datatype=None,sig=None,output=None,callback=None):
    """获取高德API的输入提示功能"""
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "type":type,
        "location":location,
        "city":city,
        "citylimit":citylimit,
        "datatype":datatype,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    response = requests.get(url,params=params)
    results = response.json()[ 'tips']
    return results

